<template>
  <div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="course_no"
        label="课程编号">
      </el-table-column>
      <el-table-column
        prop="course_name"
        label="课程名称">
      </el-table-column>
      <el-table-column
        prop="course_time"
        label="学时">
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="课程编号" :label-width="formLabelWidth" prop="class_no">
          <el-input v-model="form.course_no" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" :label-width="formLabelWidth" prop="class_room">
          <el-input v-model="form.course_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学时" :label-width="formLabelWidth" prop="course_time">
          <el-input v-model="form.course_time" type="number" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>


import {course_insert_edit, course_one, courseDelete, coursePage} from "@/api/course";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 5,
      total: 0,
      rules: {
        course_name: [
          {required: true, message: '请输入课程名称', trigger: 'blur'},
        ],
        course_no: [
          {required: true, message: '请输入课程编号', trigger: 'blur'}
        ],
        course_time: [
          {required: true, message: '请输入学时', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        course_no: '',
        course_name: '',
        course_time: '',
      }
    }
  },
  methods: {
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      course_one(row.id).then(response => {
        this.form = response.data.course
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        courseDelete(id).then(response => {
          this.$notify.success(response.message)
          this.coursePageData()
        })
      })
    },
    // 新增
    add() {
      this.title = '新增'
      this.dialogFormVisible = true
    },
    addOrUpDate() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          course_insert_edit(this.form).then(response => {
            this.$notify.success(response.message)
            this.dialogFormVisible = false
            this.coursePageData()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      // 重新获取分页数据
      this.currentPage = 1
      this.coursePageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.coursePageData()
    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {
        course_name: '',
        course_no: '',
        course_time: ''
      }
      this.$refs.dialogForm.clearValidate()
    },
    coursePageData() {
      coursePage({
        page: this.currentPage,
        size: this.pageSize
      }).then(response => {
        this.tableData = response.data.tableData
        this.total = response.data.total
      })
    }
  },
  created() {
    this.coursePageData()
  }
}
</script>
<style scoped lang="scss">
#searchForm {
  border-bottom: 1px solid #ccc;
  margin-bottom: 15px;
}
</style>


